﻿using System;
using SolidDemo.Business.Interfaces;
using SolidDemo.Data.Enums;
using SolidDemo.Data.ProjectData;

namespace SolidDemo.Business.Factories
{
    public class ProjectFactory : GomtuuFactory, IProjectFactory
    {
        public ProjectFactory(IGenericRepository repository, ILogRepository logService) : base(repository, logService) { }

        public Project Create(string type)
        {
            Project project;

            switch (type)
            {
                case EProjectType.SelectPump:
                    project = new PumpSelectProject();
                    break;
                default:
                    _logService.AddLogEntry("Unknown project type: " + type, ELogPriority.Error);
                    return null;
            }

            Save(project);

            return project;
        }

        public Project Load(int id)
        {
            var project = _repository.LoadById<Project>(id);

            // When further project types are added this is where navigation properties will be included via a switch statement. jb 

            return project;
        }

        public Project LoadOrCreate(int id, string type)
        {
            var project = Load(id) ?? Create(type);

            return project;
        }

        public Project Save(Project project)
        {
            project.Modified = DateTime.Now;
            _repository.Save(project);

            return project;
        }
    }
}
